<?php
/**
 * Created by PhpStorm.
 * 用户行为记录接口
 * User: caicm
 * Date: 2017/3/7
 * Time: 14:20
 */

namespace SDXApi\Controller;
use SDXApi\Service\UserBehavior;

class UserBehaviorController extends AppController {

    /**
     * 移动端安装渠道统计
     * @return mixed
     *
     */
    public function installation_count(){
        $channel = I('post.channel', 0, 'trim');  //渠道来源
        $uid = I('post.uid',0,'int');

        if(empty($channel) || empty($uid)) {
            returnApiError('参数有误！');
        }

        $data = array(
            'uid' => $uid,
            'channel' => $channel,
            'source' => get_source(),
            'add_time' => gmtime(),
            'version' => I('version','')
        );

        $val =  M('user_behavior_installation_count')->add($data);
        if($val){
            returnApiSuccess('','操作成功');
        }else{
            returnApiError('操作失败');
        }

    }

    /*
     * 用户页面浏览行为
     * Created By caicm
     */
    public function add_browse(){
        $uid = I('post.uid',0,'int');
        $page_id  = I('post.page_id',0,'int');
        $title_id = I('title_id',0,'int');
        $title = I('title');
        $utm_source = I('post.utm_source'); //广告来源

        if(!$page_id) returnApiError('参数有误！');

        $user_bevavior = new UserBehavior();
        $val = $user_bevavior->add_browse($uid, $page_id, $title_id, $title, $utm_source);
        if($val){
            returnApiSuccess('','操作成功');
        }else{
            returnApiError('操作失败');
        }
    }

    /*
     * 添加地址到操作记录表
     */
    public function address(){

        $address_id = I('post.address_id',0,'int');
        $page_id  = I('post.page_id',0,'int');
        if(!$page_id || !$address_id) returnApiError('参数有误！');

        $user_behavior = new UserBehavior();

        $address = $user_behavior->add_address($address_id, $page_id);

        if($address){
            returnApiSuccess('','操作成功');
        }else{
            returnApiError('操作失败');
        }

    }

    /*
     * 分享行为记录
     */
    public function add_share(){

        $event_id  = I('post.event_id',0,'int');    //事件（1：微信好友，2：微信朋友圈）
        $type_id  = I('post.type_id',0,'int');    //类型(1:专题，2：专辑，3：商品)
        $uid  = I('post.uid',0,'int');    //用户id

        if(!$event_id || !$type_id || !$uid) returnApiError('参数有误！');

        //用户名称
        $username = M("users")->where("user_id={$uid}")->getField('user_name');

        $data = array(
            'event_id' => $event_id ,
            'type_id' => $type_id ,
            'uid' => $uid ,
            'title' => I('post.title') ,
            'title_id' => I('post.title_id',0,'int'),
            'version' => I('post.version') ,
            'source' => get_source() ,
            'username' => $username,
            'add_time' => gmtime()
        );

        $val = M('user_behavior_share')->add($data);

        if($val){
            returnApiSuccess(null,'操作成功');
        }else{
            returnApiError('操作失败');
        }

    }

    /**
     * 母亲节活动
     * @author Min
     */
    public function add_motherDay_activity(){
        $uid  = I('post.uid',0,'int');
        $source  = I('post.source');
        $type = I('post.type',1,'int');
        $share_uid  = I('post.share_uid',0,'int');

        if(!$source || !$uid) returnApiError('参数有误！');

        $data = array(
            'source' => $source ,
            'share_uid' => $share_uid ,
            'uid' => $uid ,
            'type' => $type,
            'add_time' => gmtime()
        );

        $val = M('user_behavior_mother_day')->add($data);

        if($val){
            returnApiSuccess(null,'操作成功');
        }else{
            returnApiError('操作失败');
        }

    }
}